using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;
using Coniometer.Model;
using Newtonsoft.Json;

namespace Coniometer.Service
{
    public class AppDataServiceImpl : IAppDataService
    {
        private readonly string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "application.json");
        private readonly Dictionary<string, object> _data = new Dictionary<string, object>();

        public AppDataServiceImpl()
        {
            if (File.Exists(_filePath))
            {
                var json = File.ReadAllText(_filePath);
                _data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json) ??
                        new Dictionary<string, object>();
            }
        }

        public List<NavigationMenu> GetNavigationMenu()
        {
            return new List<NavigationMenu>
            {
                new NavigationMenu
                {
                    IconImage = new BitmapImage(new Uri(@"..\..\Images\nav_simple.png", UriKind.Relative)),
                    SelectedIconImage =
                        new BitmapImage(new Uri(@"..\..\Images\nav_simple_selected.png", UriKind.Relative)),
                    Title = "采样"
                },
                new NavigationMenu
                {
                    IconImage = new BitmapImage(new Uri(@"..\..\Images\nav_data.png", UriKind.Relative)),
                    SelectedIconImage =
                        new BitmapImage(new Uri(@"..\..\Images\nav_data_selected.png", UriKind.Relative)),
                    Title = "数据"
                },
                new NavigationMenu
                {
                    IconImage = new BitmapImage(new Uri(@"..\..\Images\nav_task.png", UriKind.Relative)),
                    SelectedIconImage =
                        new BitmapImage(new Uri(@"..\..\Images\nav_task_selected.png", UriKind.Relative)),
                    Title = "任务"
                },
                new NavigationMenu
                {
                    IconImage = new BitmapImage(new Uri(@"..\..\Images\nav_setting.png", UriKind.Relative)),
                    SelectedIconImage =
                        new BitmapImage(new Uri(@"..\..\Images\nav_setting_selected.png", UriKind.Relative)),
                    Title = "设置"
                }
            };
        }

        public List<SettingMenu> GetSettingMenu()
        {
            return new List<SettingMenu>
            {
                new SettingMenu
                {
                    Icon = "\xe691",
                    Title = "空间方案"
                },
                new SettingMenu
                {
                    Icon = "\xe635",
                    Title = "采样方案"
                },
                new SettingMenu
                {
                    Icon = "\xe60f",
                    Title = "打印方案"
                },
                new SettingMenu
                {
                    Icon = "\xe754",
                    Title = "时间设置"
                },
                new SettingMenu
                {
                    Icon = "\xe663",
                    Title = "设备信息"
                },
                new SettingMenu
                {
                    Icon = "\xe643",
                    Title = "网络配置"
                },
                new SettingMenu
                {
                    Icon = "\xe602",
                    Title = "用户管理"
                },
                new SettingMenu
                {
                    Icon = "\xe620",
                    Title = "帮助"
                },
                new SettingMenu
                {
                    Icon = "\xe607",
                    Title = "校准"
                }
            };
        }

        public List<string> GetWifiList()
        {
            return new List<string>
            {
                "gyujnk魔力", "绕太阳会更快", "vbhuiikfghi",
                "sdfsdbjkSd", "lkknzkfbsd", "TP-Link546",
                "Tenda-zfmhbs", "aeoimdsfsd", "奥克斯记得哈沙巴克"
            };
        }

        public void PutValue(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key)) return;
            _data[key] = value;
            File.WriteAllText(_filePath, JsonConvert.SerializeObject(_data));
        }

        public object GetValue(string key)
        {
            if (string.IsNullOrWhiteSpace(key)) return null;

            if (!File.Exists(_filePath)) return null;
            var json = File.ReadAllText(_filePath);
            var newData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
            if (newData == null) return null;
            _data.Clear();
            foreach (var kvp in newData)
            {
                _data[kvp.Key] = kvp.Value;
            }

            _data.TryGetValue(key, out var value);
            return value;
        }
    }
}